package com.angel.architecture.context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.angel.architecture.dtos.SessionUserDTO;
import com.angel.architecture.exceptions.BusinessException;
import com.angel.architecture.services.SecurityService;

public class Context {

	private HttpServletRequest request = null;

    /**
     *
     * @return current request.
     */
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * Busca en la session del request, la session del usuario.
	 *
	 * @return la Sesion del Usuario
	 * @throws com.angel.arquitectura.exception.BusinessException en caso de que el usuario no este logueado
	 */
	public SessionUserDTO getSessionUserDTO() throws BusinessException {

		SessionUserDTO sesionUsuario = (SessionUserDTO) request.getSession(true).getAttribute(SecurityService.SESION_USUARIO);
		if (sesionUsuario == null) {
			throw new BusinessException("User not logged.");
		}
		return sesionUsuario;
	}

    public void invalidateSession() {
        if (request != null) {
            HttpSession session = request.getSession(false);
            if (session != null) {
                session.invalidate();
            }
        }
    }
}
